/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.calendar.WellKnownTimeZones;
/*     */ import com.zimbra.common.calendar.ZCalendar;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZICalendarParseHandler;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import net.fortuna.ical4j.data.ParserException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class IcsImportParseHandler
/*     */   implements ZCalendar.ZICalendarParseHandler
/*     */ {
/*  52 */   ZCalendar.ZVCalendar mCurCal = null;
/*  53 */   List<ZCalendar.ZComponent> mComponents = new ArrayList();
/*  54 */   ZCalendar.ZProperty mCurProperty = null;
/*     */   
/*     */   private int mNumCals;
/*     */   
/*     */   private boolean mInZCalendar;
/*     */   private Account mAccount;
/*     */   private boolean mContinueOnError;
/*     */   private String mMethod;
/*     */   private TimeZoneMap mTimeZoneMap;
/*     */   private Set<String> mTZIDsSeen;
/*     */   private Invite.InviteVisitor mInviteVisitor;
/*     */   
/*     */   public IcsImportParseHandler(OperationContext ctxt, Account account, Folder folder, boolean continueOnError, boolean preserveExistingAlarms)
/*     */   {
/*  68 */     this.mAccount = account;
/*  69 */     this.mContinueOnError = continueOnError;
/*  70 */     this.mTZIDsSeen = new HashSet();
/*  71 */     this.mInviteVisitor = new ImportInviteVisitor(ctxt, folder, preserveExistingAlarms);
/*     */   }
/*     */   
/*     */   public IcsImportParseHandler(OperationContext ctxt, Account account, Invite.InviteVisitor visitor, boolean continueOnError, boolean removeAlarms)
/*     */   {
/*  76 */     this.mAccount = account;
/*  77 */     this.mContinueOnError = continueOnError;
/*  78 */     this.mTZIDsSeen = new HashSet();
/*  79 */     this.mInviteVisitor = visitor;
/*     */   }
/*     */   
/*     */   public void startCalendar() throws ParserException {
/*  83 */     this.mComponents.clear();
/*  84 */     this.mInZCalendar = true;
/*  85 */     this.mCurCal = new ZCalendar.ZVCalendar();
/*     */     
/*  87 */     this.mMethod = ZCalendar.ICalTok.PUBLISH.toString();
/*  88 */     this.mTimeZoneMap = new TimeZoneMap(Util.getAccountTimeZone(this.mAccount));
/*     */   }
/*     */   
/*     */   public void endCalendar() throws ParserException {
/*  92 */     this.mInZCalendar = false;
/*  93 */     this.mNumCals += 1;
/*  94 */     this.mCurCal = null;
/*     */   }
/*     */   
/*  97 */   public boolean inZCalendar() { return this.mInZCalendar; }
/*  98 */   public int getNumCals() { return this.mNumCals; }
/*     */   
/*     */   public void startComponent(String name) {
/* 101 */     if (this.mComponents.isEmpty())
/* 102 */       this.mTZIDsSeen.clear();
/* 103 */     this.mComponents.add(new ZCalendar.ZComponent(name));
/*     */   }
/*     */   
/*     */   public void endComponent(String name) throws ParserException {
/* 107 */     if (this.mComponents.isEmpty()) {
/* 108 */       throw new ParserException("Found END:" + name + " without BEGIN");
/*     */     }
/* 110 */     ZCalendar.ZComponent comp = (ZCalendar.ZComponent)this.mComponents.remove(this.mComponents.size() - 1);
/* 111 */     if (this.mComponents.size() == 0) {
/* 112 */       ZCalendar.ICalTok tok = comp.getTok();
/* 113 */       if (tok != null) {
/*     */         try {
/* 115 */           switch (tok) {
/*     */           case VEVENT: 
/*     */           case VTODO: 
/* 118 */             doComp(comp);
/* 119 */             break;
/*     */           case VTIMEZONE: 
/* 121 */             ICalTimeZone tz = ICalTimeZone.fromVTimeZone(comp);
/* 122 */             this.mTimeZoneMap.add(tz);
/*     */           }
/*     */         }
/*     */         catch (ServiceException e) {
/* 126 */           throw new ParserException("Error while parsing " + tok.toString(), e);
/*     */         }
/*     */       }
/*     */     } else {
/* 130 */       ((ZCalendar.ZComponent)this.mComponents.get(this.mComponents.size() - 1)).addComponent(comp);
/*     */     }
/*     */   }
/*     */   
/*     */   public void startProperty(String name) {
/* 135 */     this.mCurProperty = new ZCalendar.ZProperty(name);
/*     */     
/* 137 */     if (this.mComponents.size() > 0) {
/* 138 */       ((ZCalendar.ZComponent)this.mComponents.get(this.mComponents.size() - 1)).addProperty(this.mCurProperty);
/*     */     } else {
/* 140 */       this.mCurCal.addProperty(this.mCurProperty);
/*     */     }
/*     */   }
/*     */   
/*     */   public void propertyValue(String value) throws ParserException {
/* 145 */     ZCalendar.ICalTok token = this.mCurProperty.getToken();
/* 146 */     if ((ZCalendar.ICalTok.CATEGORIES.equals(token)) || (ZCalendar.ICalTok.RESOURCES.equals(token)) || (ZCalendar.ICalTok.FREEBUSY.equals(token))) {
/* 147 */       this.mCurProperty.setValueList(ZCalendar.parseCommaSepText(value));
/*     */     } else
/* 149 */       this.mCurProperty.setValue(ZCalendar.unescape(value));
/* 150 */     if (this.mComponents.size() == 0) {
/* 151 */       if (ZCalendar.ICalTok.METHOD.equals(this.mCurProperty.getToken()))
/* 152 */         this.mMethod = value;
/* 153 */       if (ZCalendar.ICalTok.VERSION.equals(this.mCurProperty.getToken())) {
/* 154 */         if ("1.0".equals(value))
/* 155 */           throw new ParserException("vCalendar 1.0 format not supported; use iCalendar instead");
/* 156 */         if (!"2.0".equals(value))
/* 157 */           throw new ParserException("Unknow iCalendar version " + value);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 162 */   public void endProperty(String name) { this.mCurProperty = null; }
/*     */   
/*     */   public void parameter(String name, String value) {
/* 165 */     ZCalendar.ZParameter param = new ZCalendar.ZParameter(name, value);
/* 166 */     if (this.mCurProperty != null) {
/* 167 */       this.mCurProperty.addParameter(param);
/*     */       
/* 169 */       if ((ZCalendar.ICalTok.TZID.equals(param.getToken())) && (this.mCurProperty.getToken() != null))
/* 170 */         this.mTZIDsSeen.add(value);
/*     */     } else {
/* 172 */       ZimbraLog.calendar.debug("ERROR: got parameter " + name + "," + value + " outside of Property");
/*     */     }
/*     */   }
/*     */   
/*     */   private void doComp(ZCalendar.ZComponent comp) throws ServiceException
/*     */   {
/* 178 */     TimeZoneMap tzmap = null;
/* 179 */     for (String tzid : this.mTZIDsSeen) {
/* 180 */       ICalTimeZone tz = this.mTimeZoneMap.getTimeZone(tzid);
/* 181 */       if (tz == null)
/*     */       {
/*     */ 
/* 184 */         String sanitizedTzid = TimeZoneMap.sanitizeTZID(tzid);
/* 185 */         tz = WellKnownTimeZones.getTimeZoneById(sanitizedTzid);
/*     */       }
/* 187 */       if (tz != null) {
/* 188 */         if (tzmap == null)
/* 189 */           tzmap = new TimeZoneMap(tz);
/* 190 */         tzmap.add(tz);
/*     */       } else {
/* 192 */         throw ServiceException.PARSE_ERROR("TZID reference encountered before/without its VTIMEZONE: " + tzid, null);
/*     */       }
/*     */     }
/*     */     
/* 196 */     if (tzmap == null) {
/* 197 */       tzmap = new TimeZoneMap(this.mTimeZoneMap.getLocalTimeZone());
/*     */     }
/* 199 */     List<ZCalendar.ZComponent> comps = new ArrayList(1);
/* 200 */     comps.add(comp);
/* 201 */     Invite.createFromCalendar(this.mAccount, null, this.mMethod, tzmap, comps.iterator(), true, this.mContinueOnError, this.mInviteVisitor);
/*     */   }
/*     */   
/*     */   public static class ImportInviteVisitor implements Invite.InviteVisitor
/*     */   {
/*     */     private OperationContext mCtxt;
/*     */     private Folder mFolder;
/*     */     private boolean mPreserveExistingAlarms;
/* 209 */     private Set<String> mUidsSeen = new HashSet();
/*     */     
/*     */     public ImportInviteVisitor(OperationContext ctxt, Folder folder, boolean preserveExistingAlarms) {
/* 212 */       this.mCtxt = ctxt;
/* 213 */       this.mFolder = folder;
/* 214 */       this.mPreserveExistingAlarms = preserveExistingAlarms;
/*     */     }
/*     */     
/*     */     public void visit(Invite inv) throws ServiceException
/*     */     {
/* 219 */       String uid = inv.getUid();
/* 220 */       if (uid == null) {
/* 221 */         uid = LdapUtil.generateUUID();
/* 222 */         inv.setUid(uid);
/*     */       }
/*     */       boolean addRevision;
/* 225 */       if (!this.mUidsSeen.contains(uid)) {
/* 226 */         boolean addRevision = true;
/* 227 */         this.mUidsSeen.add(uid);
/*     */       } else {
/* 229 */         addRevision = false;
/*     */       }
/*     */       try
/*     */       {
/* 233 */         this.mFolder.getMailbox().addInvite(this.mCtxt, inv, this.mFolder.getId(), this.mPreserveExistingAlarms, addRevision);
/* 234 */         if (ZimbraLog.calendar.isDebugEnabled()) {
/* 235 */           if (inv.isEvent()) {
/* 236 */             ZimbraLog.calendar.debug("Appointment imported: UID=" + inv.getUid());
/* 237 */           } else if (inv.isTodo())
/* 238 */             ZimbraLog.calendar.debug("Task imported: UID=" + inv.getUid());
/*     */         }
/*     */       } catch (ServiceException se) {
/* 241 */         if (se.getCode().equals("service.FORBIDDEN")) {
/* 242 */           Invite.logIcsParseImportError(inv, se);
/*     */         } else {
/* 244 */           throw se;
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/IcsImportParseHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */